<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>The source code</title>
  <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  <style type="text/css">
    .highlight { display: block; background-color: #ddd; }
  </style>
  <script type="text/javascript">
    function highlight() {
      document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
    }
  </script>
</head>
<body onload="prettyPrint(); highlight();">
  <pre class="prettyprint lang-js">&lt;?php
namespace Library\Dal;

abstract class Model
{
	protected $primaryKey;
	protected $connection;
	protected $table;
	
	private $_adapter;
	private $_reflection;
	private $_config;
	
	private static $_dbSettings;
	
	public function __construct($data, $forceLoading = true)
	{
		$this-&gt;_reflection = new \ReflectionClass($this);
		$this-&gt;_adapter = new \Library\Dal\Driver\Mysql\Adapter($this-&gt;connection);
		$this-&gt;_config = \Library\Application::getInstance()-&gt;config;
		
		if (!is_array($data))
		{
			$this-&gt;_reflection-&gt;getProperty($this-&gt;primaryKey)-&gt;setValue($this, $data);
		}
		else
		{
			foreach($data as $key =&gt; $value)
			{
				$this-&gt;_reflection-&gt;getProperty($key)-&gt;setValue($this, $value);
			}
		}
		
		if ($forceLoading)
		{
			$this-&gt;load();
		}
	}
	
	private function _readDbSettings()
	{
		$settings = array();
		
		if (file_exists($this-&gt;_config['application']['path'] . '/config/database.ini'))
		{
			$ini = new \Library\Config\Ini($this-&gt;_config['application']['path'] . '/config/database.ini');
			$settings = $ini-&gt;getData();
		}
		
		return $settings;
	}
	
	private function _getDbSettings()
	{
		if (self::$_dbSettings === null)
		{
			self::$_dbSettings = $this-&gt;_readDbSettings();
		}

		return self::$_dbSettings;
	}
	
	public function load()
	{
		$fields = $this-&gt;_reflection-&gt;getProperties(\ReflectionProperty::IS_PUBLIC);
		$activeRecord = new \Library\Dal\Driver\Mysql\ActiveRecord();
		$arFields = array();
		
		foreach($fields as $prop)
		{
			if ($prop-&gt;getDeclaringClass()-&gt;getName() != get_class($this))
			{
				continue;
			}

			$arFields[] = $prop-&gt;getName();
		}
		
		$query = $activeRecord-&gt;select($arFields)
							  -&gt;from($this-&gt;table)
							  -&gt;where($this-&gt;primaryKey . ' = :pkey')
							  -&gt;bind(':pkey', $this-&gt;_reflection-&gt;getProperty($this-&gt;primaryKey)-&gt;getValue($this));
							  
		$result = $this-&gt;_adapter-&gt;fetchRow($query);
		
		if (is_array($result))
		{
			foreach($result as $key =&gt; $value)
			{
				if ($this-&gt;_reflection-&gt;hasProperty($key))
				{
					$this-&gt;_reflection-&gt;getProperty($key)-&gt;setValue($this, $value);
				}
			}
		}
	}
	
	public function save()
	{
		
	}
	
	public function delete()
	{
		
	}
}</pre>
</body>
</html>
